(load "init.ss")
(define (sqrt x)
  (newton (lambda (y)
            (- x (* y y)))
          1.0))  ;; -1.0 will lead to y < 0

;; fixed point
(define (fixed-point fn guess)
  (define (iter old new)
    (if (< (abs (- old new)) 0.0001) ;; good enough
      new
      (iter new (fn new))))
  (iter guess (fn guess)))

;; 牛顿法
(define (newton fn guess)
  (define df (deriv fn))
  (fixed-point
    (lambda (x) (- x (/ (fn x)(df x))))
    guess))

;; 求导
(define (deriv f)
    (lambda (x)
      (/ (- (f (+ x dx))
            (f x))
         dx)))

(define dx 0.000001)
